﻿#include "safetysettingwid.h"
#include "ui_safetysettingwid.h"
#include "cfgjsonrw.h"
#include "gratingsettingwid.h"
#include "paramadddialog.h"

SafetySettingWid::SafetySettingWid(QWidget *parent)
	: QWidget(parent)
	, ui(new Ui::SafetySettingWid())
{
	ui->setupUi(this);
	setStyleSheet("font: 12pt \"Agency FB\"");
}

SafetySettingWid::~SafetySettingWid()
{
	delete ui;
}


void SafetySettingWid::Init()
{
	InitGrating();
	InitSafeDoor();
	InitAlarmLight();
	InitSafeLock();
}

/*光栅配置*/
void SafetySettingWid::InitGrating()
{
	ui->tabWidget->clear();
	auto gras = cfgrw->GetGratingCfg();
	for (auto& iter : gras) {
		auto tem = new GratingSettingWid(this);
		tem->Init(iter);
		ui->tabWidget->addTab(tem, iter.id());
	}
}

void SafetySettingWid::InitSafeDoor()
{
	auto cfg = cfgrw->GetSafeDoorCfg();
	ui->checkBox->setCheckState(cfg.enable() == 1 ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
	ui->spinBox->setValue(cfg.stop());
	ui->spinBox_2->setValue(cfg.reset());
	ui->spinBox_3->setValue(cfg.start());
	ui->spinBox_4->setValue(cfg.enter());

}
void SafetySettingWid::InitAlarmLight()
{
	auto cfg = cfgrw->GetAlarmLightCfg();
	ui->checkBox_2->setCheckState(cfg.enable() == 1 ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
	ui->spinBox_5->setValue(cfg.red());
	ui->spinBox_6->setValue(cfg.green());
	ui->spinBox_7->setValue(cfg.yellow());
}
void SafetySettingWid::InitSafeLock()
{
	auto cfg = cfgrw->GetSafeLockerCfg();
	ui->checkBox_3->setCheckState(cfg.enable() == 1 ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
	ui->spinBox_8->setValue(cfg.set());
	ui->spinBox_9->setValue(cfg.state());
}


void SafetySettingWid::on_pushButton_gra_add_clicked()
{
	QStringList param = { "光栅编号:" };
	ParamAddDialog dlg(param, this);
	dlg.exec();
	auto ret = dlg.GetParam();
	if (ret.size() != param.size()) {
		QMessageBox::warning(this, "ERROR", "光栅编号为空，添加失败");
		return;
	}
	QString name = ret[0];
	auto tmp = new GratingSettingWid(this);
	GratingJ setting = {};
	setting.id = name;
	tmp->Init(setting);
	ui->tabWidget->addTab(tmp, name);
}

void SafetySettingWid::on_pushButton_gra_del_clicked()
{
	GratingSettingWid* tmp = qobject_cast<GratingSettingWid*>(ui->tabWidget->currentWidget());
	if (tmp == nullptr) {
		QMessageBox::warning(this, "ERROR", "当前光栅窗口为空无法执行删除操作");
		return;
	}
	auto cfg = tmp->GetCfg();
	auto ret = QMessageBox::question(this, "QUESTION", QString("确认删除%1, 删除后数据无法恢复").arg(cfg.id()));
	if (ret == QMessageBox::StandardButton::Yes) {
		ui->tabWidget->removeTab(ui->tabWidget->currentIndex());
		tmp->deleteLater();
	}
}

void SafetySettingWid::on_pushButton_gra_save_clicked()
{
	QList<GratingJ> setting;
	for (int i = 0; i < ui->tabWidget->count(); i++) {
		GratingSettingWid* tmp = qobject_cast<GratingSettingWid*>(ui->tabWidget->widget(i));
		if (tmp == nullptr) {
			ui->tabWidget->removeTab(i);
			continue;
		}
		setting.append(tmp->GetCfg());
	}
	if (!cfgrw->SaveGratingCfg(setting)) {
		QMessageBox::warning(this, "ERROR", "保存失败，请重新保存");
	}
	InitGrating();
}

/*保存安全门*/
void SafetySettingWid::on_pushButton_1_clicked()
{
	SafeDoorJ cfg = {};
	cfg.enable() = ui->checkBox->checkState() == Qt::CheckState::Checked ? 1 : 0;
	cfg.stop() = ui->spinBox->value();
	cfg.reset() = ui->spinBox_2->value();
	cfg.start() = ui->spinBox_3->value();
	cfg.enter() = ui->spinBox_4->value();
	if (!cfgrw->SaveSafeDoorCfg(cfg)) {
		QMessageBox::warning(this, "ERROR", "保存失败，请重新保存");
	}
	InitSafeDoor();
}
/*保存报警灯*/
void SafetySettingWid::on_pushButton_2_clicked()
{
	AlarmJ cfg = {};
	cfg.enable() = ui->checkBox_2->checkState() == Qt::CheckState::Checked ? 1 : 0;
	cfg.red() = ui->spinBox_5->value();
	cfg.green() = ui->spinBox_6->value();
	cfg.yellow() = ui->spinBox_7->value();
	if (!cfgrw->SaveAlarmLightCfg(cfg)) {
		QMessageBox::warning(this, "ERROR", "保存失败，请重新保存");
	}
	InitAlarmLight();
}
// 保存安全锁
void SafetySettingWid::on_pushButton_3_clicked()
{
	SafeLockJ cfg = {};
	cfg.enable() = ui->checkBox_3->checkState() == Qt::CheckState::Checked ? 1 : 0;
	cfg.set() = ui->spinBox_8->value();
	cfg.state() = ui->spinBox_9->value();
	if (!cfgrw->SaveSafeLockerCfg(cfg)) {
		QMessageBox::warning(this, "ERROR", "保存失败，请重新保存");
	}
	InitSafeLock();
}
